package com.it.gw.modules.water.viewmodel

import android.util.Log
import com.it.common.bean.CommonBean
import com.it.common.bean.ResultBean
import com.it.common.livedata.UnPeekLiveData
import com.it.data.base.BaseViewModel
import com.it.data.network.Api
import com.it.data.network.req.DouQItemsDto
import com.it.data.network.req.DouQRecordDto
import com.it.data.network.req.IrrItemsDto
import com.it.data.network.req.IrrRecordDto
import com.it.data.network.req.LakeItemsDto
import com.it.data.network.req.LakeRecordDto
import com.it.data.network.req.PumpItemsDto
import com.it.data.network.req.PumpRecordDto
import com.it.data.network.req.ResItemsDto
import com.it.data.network.req.ResRecordDto
import com.it.data.network.req.RiverRecordDto
import com.it.data.network.req.RvItemsDto
import com.it.data.network.req.ShouBuItemsDto
import com.it.data.network.req.ShouBuRecordDto
import com.it.data.network.req.SluiceItemsDto
import com.it.data.network.req.SluiceRecordDto
import com.it.data.network.req.UpdateRsvrInfoDto
import com.it.data.network.req.UploadFileDto
import com.it.data.network.req.WellItemsDto
import com.it.data.network.req.WellRecordDto
import com.it.data.network.rsp.CheckStateVo
import com.it.data.network.rsp.PageVo
import com.it.data.network.rsp.RsvrDetailVo
import com.it.data.network.rsp.RsvrVo
import com.it.data.network.rsp.WaterRecordVo
import com.it.data.utils.HttpParamUtil


class WaterViewModel : BaseViewModel() {

    val insertLiveData by lazy { UnPeekLiveData<CommonBean<Any>>() }

    //水库
    val resRecordDtoListLiveData by lazy { UnPeekLiveData<CommonBean<PageVo<List<ResRecordDto>>>>() }
    val resRecordDtoDetailLiveData by lazy { UnPeekLiveData<CommonBean<ResRecordDto>>() }

    //河流
    val riverRecordDtoListLiveData by lazy { UnPeekLiveData<CommonBean<PageVo<List<RiverRecordDto>>>>() }
    val riverRecordDtoDetailLiveData by lazy { UnPeekLiveData<CommonBean<RiverRecordDto>>() }

    //湖泊
    val lakeRecordDtoListLiveData by lazy { UnPeekLiveData<CommonBean<PageVo<List<LakeRecordDto>>>>() }
    val lakeRecordDtoDetailLiveData by lazy { UnPeekLiveData<CommonBean<LakeRecordDto>>() }

    //水闸
    val sluiceRecordDtoListLiveData by lazy { UnPeekLiveData<CommonBean<PageVo<List<SluiceRecordDto>>>>() }
    val sluiceRecordDtoDetailLiveData by lazy { UnPeekLiveData<CommonBean<SluiceRecordDto>>() }

    //泵站
    val pumpRecordDtoListLiveData by lazy { UnPeekLiveData<CommonBean<PageVo<List<PumpRecordDto>>>>() }
    val pumpRecordDtoDetailLiveData by lazy { UnPeekLiveData<CommonBean<PumpRecordDto>>() }

    //首部
    val shouBuRecordDtoListLiveData by lazy { UnPeekLiveData<CommonBean<PageVo<List<ShouBuRecordDto>>>>() }
    val shouBuRecordDtoDetailLiveData by lazy { UnPeekLiveData<CommonBean<ShouBuRecordDto>>() }

    //斗渠
    val douQRecordDtoListLiveData by lazy { UnPeekLiveData<CommonBean<PageVo<List<DouQRecordDto>>>>() }
    val douQRecordDtoDetailLiveData by lazy { UnPeekLiveData<CommonBean<DouQRecordDto>>() }


    //机井
    val wellRecordDtoListLiveData by lazy { UnPeekLiveData<CommonBean<PageVo<List<WellRecordDto>>>>() }
    val wellRecordDtoDetailLiveData by lazy { UnPeekLiveData<CommonBean<WellRecordDto>>() }

    //灌区
    val irrRecordDtoListLiveData by lazy { UnPeekLiveData<CommonBean<PageVo<List<IrrRecordDto>>>>() }
    val irrRecordDtoDetailLiveData by lazy { UnPeekLiveData<CommonBean<IrrRecordDto>>() }

    val rsvrVoLiveData by lazy { UnPeekLiveData<CommonBean<List<RsvrVo>>>() }

    val rsvrDetailVoLiveData by lazy { UnPeekLiveData<CommonBean<List<RsvrDetailVo>>>() }


    val updateRsvrInfoLiveData by lazy { UnPeekLiveData<CommonBean<Any>>() }


    val rsvrByStcdAndTmLiveData by lazy { UnPeekLiveData<CommonBean<UpdateRsvrInfoDto>>() }

    val checkStateVoLiveData by lazy { UnPeekLiveData<CommonBean<CheckStateVo>>() }

    val waterRecordVoLiveData by lazy { UnPeekLiveData<CommonBean<WaterRecordVo>>() }

    /**
     * 获取水库列表
     * @param pageNum Int
     * @param pageSize Int
     * @param order String?
     * @param resName String?
     * @param secondName String?
     * @param extent List<String>?
     * @param addvcd String?
     */
    fun getResItems(
        pageNum: Int,
        pageSize: Int,
        order: String? = null,
        resName: String? = null,
        resType: String? = null,
        secondName: String? = null,
        extent: List<String>? = null,
        addvcd: String? = null,
    ) {
        val dto = ResItemsDto(
            pageNum = pageNum,
            pageSize = pageSize,
            order = order,
            resName = resName,
            resType = resType,
            secondName = secondName,
            extent = extent,
            addvcd = addvcd
        )
        requestCommit(request = {
            Api.apiClient()
                .getResItems(
                    dto = dto
                )
        }, resp = {
            resRecordDtoListLiveData.postValue(it)
        })
    }


    fun getResBasic(
        id: String
    ) {
        requestCommit(request = {
            Api.apiClient()
                .getResBasic(
                    id = id
                )
        }, resp = {
            resRecordDtoDetailLiveData.postValue(it)
        })
    }

    fun insertResRecord(dto: ResRecordDto) {
        requestCommit(request = {
            Api.apiClient()
                .insertResRecord(
                    dto = dto
                )
        }, resp = {
            insertLiveData.postValue(it)
        })
    }

    /**
     * 河流列表
     * @param pageNum Int
     * @param pageSize Int
     * @param order String?
     * @param name String?
     * @param secondName String?
     * @param extent List<String>?
     * @param rvGrad String?
     */
    fun getRvItems(
        pageNum: Int,
        pageSize: Int,
        order: String? = null,
        name: String? = null,
        secondName: String? = null,
        extent: List<String>? = null,
        rvGrad: String? = null,
    ) {
        val dto = RvItemsDto(
            pageNum = pageNum,
            pageSize = pageSize,
            order = order,
            rvName = name,
            secondName = secondName,
            extent = extent,
            rvGrad = rvGrad
        )
        requestCommit(request = {
            Api.apiClient()
                .getRvItems(
                    dto = dto
                )
        }, resp = {
            riverRecordDtoListLiveData.postValue(it)
        })
    }

    fun insertRiverRecord(dto: RiverRecordDto) {
        requestCommit(request = {
            Api.apiClient()
                .insertRiverRecord(
                    dto = dto
                )
        }, resp = {
            insertLiveData.postValue(it)
        })
    }

    fun getRiveBasic(
        id: String
    ) {
        requestCommit(request = {
            Api.apiClient()
                .getRiveBasic(
                    id = id
                )
        }, resp = {
            riverRecordDtoDetailLiveData.postValue(it)
        })
    }


    fun getLakeItems(
        pageNum: Int,
        pageSize: Int,
        order: String? = null,
        name: String? = null,
        secondName: String? = null,
        addvcd: String? = null,
        extent: List<String>? = null,
        rvGrad: String? = null,
    ) {
        val dto = LakeItemsDto(
            pageNum = pageNum,
            pageSize = pageSize,
            order = order,
            lkName = name,
            secondName = secondName,
            extent = extent,
            addvcd = addvcd
        )
        requestCommit(request = {
            Api.apiClient()
                .getLakeItems(
                    dto = dto
                )
        }, resp = {
            lakeRecordDtoListLiveData.postValue(it)
        })
    }

    /**
     * 湖泊详情
     * @param id String
     */
    fun getLakeBasic(
        id: String
    ) {
        requestCommit(request = {
            Api.apiClient()
                .getLakeBasic(
                    id = id
                )
        }, resp = {
            lakeRecordDtoDetailLiveData.postValue(it)
        })
    }

    /**
     * 添加/修改湖泊
     * @param dto LakeRecordDto
     */
    fun insertLakeRecord(dto: LakeRecordDto) {
        requestCommit(request = {
            Api.apiClient()
                .insertLakeRecord(
                    dto = dto
                )
        }, resp = {
            insertLiveData.postValue(it)
        })
    }

    ////////水闸
    fun getSluiceItems(
        pageNum: Int,
        pageSize: Int,
        order: String? = null,
        name: String? = null,
        secondName: String? = null,
        wagaType: String? = null,
        addvcd: String? = null,
        extent: List<String>? = null,
        rvGrad: String? = null,
    ) {
        val dto = SluiceItemsDto(
            pageNum = pageNum,
            pageSize = pageSize,
            order = order,
            sluiceName = name,
            secondName = secondName,
            wagaType = wagaType,
            extent = extent,
            addvcd = addvcd
        )
        requestCommit(request = {
            Api.apiClient()
                .getSluiceItems(
                    dto = dto
                )
        }, resp = {
            sluiceRecordDtoListLiveData.postValue(it)
        })
    }

    fun getSluiceBasic(
        id: String
    ) {
        requestCommit(request = {
            Api.apiClient()
                .getSluiceBasic(
                    id = id
                )
        }, resp = {
            sluiceRecordDtoDetailLiveData.postValue(it)
        })
    }
    fun getAfterSluiceParam(
        guid: String?,
        type: String?
    ) {
        requestCommit(request = {
            Api.apiClient()
                .getAfterSluiceParam(
                    guid = guid,
                    type = type
                )
        }, resp = {
            sluiceRecordDtoDetailLiveData.postValue(it)
        })
    }

    //水闸增删改申请
    fun sluiceApply(dto: SluiceRecordDto) {
        val list = mutableListOf<SluiceRecordDto>()
        list.add(dto)
        requestCommit(request = {
            Api.apiClient()
                .sluiceApply(
                    list = list
                )
        }, resp = {
            insertLiveData.postValue(it)
        })
    }


    ////////泵站
    fun getPumpItems(
        pageNum: Int,
        pageSize: Int,
        order: String? = null,
        name: String? = null,
        secondName: String? = null,
        addvcd: String? = null,
        pustType: String? = null,

        extent: List<String>? = null,
        rvGrad: String? = null,
    ) {
        val dto = PumpItemsDto(
            pageNum = pageNum,
            pageSize = pageSize,
            order = order,
            pumpName = name,
            secondName = secondName,
            extent = extent,
            addvcd = addvcd,
            pustType = pustType
        )
        requestCommit(request = {
            Api.apiClient()
                .getPumpItems(
                    dto = dto
                )
        }, resp = {
            pumpRecordDtoListLiveData.postValue(it)
        })
    }

    fun getPumpBasic(
        id: String
    ) {
        requestCommit(request = {
            Api.apiClient()
                .getPumpBasic(
                    id = id
                )
        }, resp = {
            pumpRecordDtoDetailLiveData.postValue(it)
        })
    }
    fun getAfterPumpParam(
        guid: String?,
        type: String?
    ) {
        requestCommit(request = {
            Api.apiClient()
                .getAfterPumpParam(
                    guid = guid,
                    type = type
                )
        }, resp = {
            pumpRecordDtoDetailLiveData.postValue(it)
        })
    }

    fun insertPumpRecord(dto: PumpRecordDto) {
        val list = mutableListOf<PumpRecordDto>()
        list.add(dto)
        Log.d(TAG, "insertPumpRecord: ${dto}")
        requestCommit(request = {
            Api.apiClient()
                .insertPumpRecord(
                    list = list
                )
        }, resp = {
            insertLiveData.postValue(it)
        })
    }

    ////////首部
    fun getShouBuItems(
        pageNum: Int,
        pageSize: Int,
        order: String? = null,
        name: String? = null,
        secondName: String? = null,
        addvcd: String? = null,
        extent: List<String>? = null,
        sblx: String? = null,//首部类型
        rvGrad: String? = null,
    ) {
        val dto = ShouBuItemsDto(
            pageNum = pageNum,
            pageSize = pageSize,
            order = order,
            shouBuName = name,
            secondName = secondName,
            extent = extent,
            addvcd = addvcd,
            sblx = sblx
        )
        requestCommit(request = {
            Api.apiClient()
                .getShouBuItems(
                    dto = dto
                )
        }, resp = {
            shouBuRecordDtoListLiveData.postValue(it)
        })
    }

    fun getShouBuBasic(
        id: String
    ) {
        requestCommit(request = {
            Api.apiClient()
                .getShouBuBasic(
                    id = id
                )
        }, resp = {
            shouBuRecordDtoDetailLiveData.postValue(it)
        })
    }

    fun getAfterShouBuParam(
        guid: String?,
        type: String?
    ) {
        requestCommit(request = {
            Api.apiClient()
                .getAfterShouBuParam(
                    guid = guid,
                    type = type
                )
        }, resp = {
            shouBuRecordDtoDetailLiveData.postValue(it)
        })
    }


    fun insertShouBuRecord(dto: ShouBuRecordDto) {
        val list = mutableListOf<ShouBuRecordDto>()
        list.add(dto)
        requestCommit(request = {
            Api.apiClient()
                .insertShouBuRecord(
                    list = list
                )
        }, resp = {
            insertLiveData.postValue(it)
        })
    }


    ////////斗渠
    fun getDouQItems(
        pageNum: Int,
        pageSize: Int,
        order: String? = null,
        name: String? = null,
        secondName: String? = null,
        addvcd: String? = null,
        extent: List<String>? = null,
        rvGrad: String? = null,
    ) {
        val dto = DouQItemsDto(
            pageNum = pageNum,
            pageSize = pageSize,
            order = order,
            douQuName = name,
            secondName = secondName,
            extent = extent,
            addvcd = addvcd
        )
        requestCommit(request = {
            Api.apiClient()
                .getDouQItems(
                    dto = dto
                )
        }, resp = {
            douQRecordDtoListLiveData.postValue(it)
        })
    }

    fun getDouQBasic(
        id: String
    ) {
        requestCommit(request = {
            Api.apiClient()
                .getDouQBasic(
                    id = id
                )
        }, resp = {
            douQRecordDtoDetailLiveData.postValue(it)
        })
    }

    fun insertDouQRecord(dto: DouQRecordDto) {
        requestCommit(request = {
            Api.apiClient()
                .insertDouQRecord(
                    dto = dto
                )
        }, resp = {
            insertLiveData.postValue(it)
        })
    }


    ////////机井
    fun getWellItems(
        pageNum: Int,
        pageSize: Int,
        order: String? = null,
        name: String? = null,
        secondName: String? = null,
        addvcd: String? = null,
        extent: List<String>? = null,
        rvGrad: String? = null,
    ) {
        val dto = WellItemsDto(
            pageNum = pageNum,
            pageSize = pageSize,
            order = order,
            wellName = name,
            secondName = secondName,
            extent = extent,
            addvcd = addvcd
        )
        requestCommit(request = {
            Api.apiClient()
                .getWellItems(
                    dto = dto
                )
        }, resp = {
            wellRecordDtoListLiveData.postValue(it)
        })
    }

    fun getWellBasic(
        id: String
    ) {
        requestCommit(request = {
            Api.apiClient()
                .getWellBasic(
                    id = id
                )
        }, resp = {
            wellRecordDtoDetailLiveData.postValue(it)
        })
    }

    fun insertWellRecord(dto: WellRecordDto) {
        requestCommit(request = {
            Api.apiClient()
                .insertWellRecord(
                    dto = dto
                )
        }, resp = {
            insertLiveData.postValue(it)
        })
    }

    ////////灌区
    fun getIrrItems(
        pageNum: Int,
        pageSize: Int,
        order: String? = null,
        name: String? = null,
        secondName: String? = null,
        addvcd: String? = null,
        extent: List<String>? = null,
        rvGrad: String? = null,
    ) {
        val dto = IrrItemsDto(
            pageNum = pageNum,
            pageSize = pageSize,
            order = order,
            irrName = name,
            secondName = secondName,
            extent = extent,
            addvcd = addvcd
        )
        requestCommit(request = {
            Api.apiClient()
                .getIrrItems(
                    dto = dto
                )
        }, resp = {
            irrRecordDtoListLiveData.postValue(it)
        })
    }

    fun getIrrBasic(
        id: String
    ) {
        requestCommit(request = {
            Api.apiClient()
                .getIrrBasic(
                    id = id
                )
        }, resp = {
            irrRecordDtoDetailLiveData.postValue(it)
        })
    }

    fun insertIrrRecord(dto: IrrRecordDto) {
        requestCommit(request = {
            Api.apiClient()
                .insertIrrRecord(
                    dto = dto
                )
        }, resp = {
            insertLiveData.postValue(it)
        })
    }

    fun selectUserManageRsvrList_APP(addvcd: String) {
        requestCommit(request = {
            Api.apiClient().selectUserManageRsvrList_APP(addvcd)
        }, resp = {
            rsvrVoLiveData.postValue(it)
        })
    }

    /**
     * 返回水库某日的填写数据
     * @param tm String
     * @param stcd String
     */
    fun getRsvrByStcdAndTm_APP(tm: String, stcd: String) {
        requestCommit(request = {
            Api.apiClient().getRsvrByStcdAndTm_APP(tm, stcd)
        }, resp = {
            rsvrByStcdAndTmLiveData.postValue(it)
        })
    }

    /**
     * 更新上报 水情
     * @param dto UpdateRsvrInfoDto
     */
    fun updateRsvrInfo(dto: UpdateRsvrInfoDto) {
        requestCommit(request = {
            Api.apiClient().updateRsvrInfo(dto)
        }, resp = {
            updateRsvrInfoLiveData.postValue(it)
        })
    }

    fun getRsvrByAddvcdAndTm_APP(
        masterCd: String,
        addvcd: String,
        tm: String,
    ) {
        requestCommit(request = {
            Api.apiClient().getRsvrByAddvcdAndTm_APP(masterCd, addvcd, tm)
        }, resp = {
            rsvrDetailVoLiveData.postValue(it)
        })
    }

    fun getCheckStateByTm(
        stcd: String?,
        date: String,
    ) {
        requestCommit(request = {
            Api.apiClient().getCheckStateByTm(stcd = stcd, date = date)
        }, resp = {
            checkStateVoLiveData.postValue(it)
        })
    }

    suspend fun upload(
        uploadFileDtoList: MutableList<UploadFileDto>
    ): ResultBean<String> {
        Log.d(TAG, "uploadPic: uploadFileDtoList${uploadFileDtoList}")
        val res = Api.apiClient()
            .upload(
                HttpParamUtil.filesToMultipartBody2(
                    vo = null,
                    uploadFileDtoList = uploadFileDtoList,
                    mediaType = "*/*"
                )
            )
        return res
    }


    fun applicationList(
        trcd: String? = null,//行政区划编码
        style: String? = null,// 要素类型WAGA水闸SHBU首部PUST泵站
        stTm: String? = null,//开始时间
        enTm: String? = null,//结束时间
        state: String? = null,//审核状态1待审核2已审核3驳回
        stnm: String? = null,//测站名称
        addvcd: String? = null,//用户行政区划编码（必传）
        pageSize: Int,//
        pageNum: Int,
    ) {
        requestCommit(request = {
            Api.apiClient().applicationList(
                trcd = trcd,
                style = style,
                stTm = stTm,
                enTm = enTm,
                state = state,
                stnm = stnm,
                addvcd = addvcd,
                pageSize = pageSize,
                pageNum = pageNum
            )
        }, resp = {
            waterRecordVoLiveData.postValue(it)


        })
    }

}